package com.weilele.leisure.time.ui.module.video_detail

import android.os.Bundle
import android.view.Gravity
import android.view.View
import com.weilele.base.library.BaseBindingFragment
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.databinding.FragmentVideoInfoBinding
import com.weilele.leisure.time.ui.holder.VideoTagHolder
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.base.helper.createViewModel
import com.weilele.mvvm.base.livedata.LiveDataWrap
import com.weilele.mvvm.base.livedata.wrapSuccessObserver
import com.weilele.mvvm.utils.activity.*

class VideoInfoFragment : BaseBindingFragment<FragmentVideoInfoBinding>() {

    private val feiLeiAdapter by lazy {
        VideoTagHolder.newInstance().apply {
            mBinding.rcvFenLei.adapter = this
        }
    }
    private val actorAdapter by lazy {
        VideoTagHolder.newInstance().apply {
            mBinding.rcvActor.adapter = this
        }
    }
    private val directAdapter by lazy {
        VideoTagHolder.newInstance().apply {
            mBinding.rcvDirect.adapter = this
        }
    }

    private val videoDetailViewModel: VideoDetailViewModel by lazy {
        requireParentFragment().createViewModel()
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(
            videoDetailViewModel.videoDetailData wrapSuccessObserver ::updateInfo
        )
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        val parentFg = parentFragment
        if (parentFg is VideoDetailFragment) {
            mBinding.cacheVideo.visible()
            mBinding.cacheVideo.onClick {
                parentFg.showCacheDialog(it)
            }
        } else {
            mBinding.cacheVideo.gone()
        }
    }

    fun updateInfo(video: VideoDetailBean) {
        mBinding.tvInfo.textFrom(video.blurb)
        mBinding.tvTitle.textFrom(video.title)
        mBinding.tvTime.textFrom("更新：${video.time}")
        feiLeiAdapter.refreshList(getVideoTag(video))
        actorAdapter.refreshList(video.actors)
        directAdapter.refreshList(video.director)
    }

    private fun getVideoTag(video: VideoDetailBean): MutableList<String>? {
        val tag = mutableListOf<String>()
        video.year?.let {
            tag.add(it)
        }
        video.area?.let {
            tag.addAll(it)
        }
        video.extTypes?.let {
            tag.addAll(it)
        }
        video.lang?.let {
            tag.addAll(it)
        }
        return tag
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.tvExpandInfo)
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.tvExpandInfo -> {
                val oldLines = mBinding.tvInfo.maxLines
                mBinding.tvInfo.maxLines = if (oldLines == 1) {
                    mBinding.tvExpandInfo.setIcon(R.drawable.icon_up_small, gravity = Gravity.END)
                    Int.MAX_VALUE
                } else {
                    mBinding.tvExpandInfo.setIcon(R.drawable.icon_down_small, gravity = Gravity.END)
                    1
                }
            }
            else -> {
            }
        }
    }
}